import Vue from "vue"
export default {
  addStoreCart(context, product) {
    // eslint-disable-next-line no-unused-vars
    return new Promise((resolve, reject) => {
      //直接返回这个结果
      let old = context.state.cartList.find((v) => v.iid == product.iid);
      if (!old) {
        product.num = 1;
        product.checked = true;
        context.commit("ADDCART", product);
        resolve("添加了新的商品");
      } else {
        context.commit("ADDCOUNT", old);
        resolve("当前的商品数量+1");
      }
    });
  },
  addCollect(context, collect) {
    // eslint-disable-next-line no-unused-vars
    return new Promise((resolve, reject) => {
      let index = context.state.collectList.findIndex(
        (v) => v.iid == collect.iid
      );

      if (index === -1) {
        collect.isCollect=true
        context.commit("ADDCOLLECT", collect);
        resolve("添加了新的收藏");
      Vue.prototype.$message.success("收藏成功");
      } else {
        collect.isCollect=false
        context.commit("DELETECOLLECT", index);
        Vue.prototype.$message.warning("取消收藏");
      }
    });
  },

  changecartcheck(context, cart) {
    context.commit("CHANGECHECKED", cart);
  },
  handledeleteProduct(context, cart) {
    let index = context.state.cartList.findIndex((v) => v.iid == cart.iid);
    console.log(index);
    context.commit("DELETEPRO", index);
  },
  deleteNum(context, cart) {
    context.commit("DELETNUM", cart);
  },
  addNum(context, cart) {
    context.commit("ADDCOUNT", cart);
  },

  // 4  进行修改数量

  // 5 设置回缓存和data中
};
